require.config({
    baseUrl: "./modules-list",
    paths: {
        jq: "../js/jquery.2.2.4",
        getJson: "../punlic1/getJson",
        render: "render",
        setLocal: "setLocal"
    }
})
require(["jq", "getJson", "render", "setLocal"], function (_, gj, r, sl) {
    console.log(gj)
    // 1. 先请求数据
    gj({
        url: "./data/goods.json"
    }).then((res) => {
        // console.log(res)
        // 2. 渲染页面
        r({
            data: JSON.parse(res),
            cont: $(".white")
        })
    })
    // 3. 绑定事件
    $(".white").on("click", ".add", function () {
        // console.log($(this).parent().attr("index"))
        let id = $(this).parent().attr("index");
        let price = $(this).prev("span").html() - 0;
        saveId = id
        savePrice = price
        // 4. 存数据
        // 要将：id,num,flag,price，存储到localStorage
        sl({ id, price, num: 1, flag: 0 });

        // 加入成功显示提示消息
        $(this).children().css('display', 'block')
        setTimeout(() => {
            $(this).children().css('display', 'none')
        }, 300)

        const token = sessionStorage.getItem("isLogin") ? JSON.parse(sessionStorage.getItem("isLogin")) : {};

            if (token.code !== "OK") {
                if (confirm("没有登录不允许打开购物车页面，是否去登录")) {
                    location.href = "../login.html"
                } else {
                    location.href = "../index.html"
                }
            }
    })

    $(".white").on("click", ".productImg", function () {
        let id = $(this).parent().attr("index");
        let price = $(this).next('p').next("span").html() - 0;
        let name = $(this).next('p').html();
        let src = this.src;
        let InfoObj = {}
        InfoObj.src = src
        InfoObj.id = id
        InfoObj.price = price
        InfoObj.name = name
        console.log(InfoObj.id, InfoObj.price)
        localStorage.removeItem("Info")
        localStorage.setItem("Info", JSON.stringify(InfoObj))
        window.open(`./product.html`, "_self")
    })
})